/*
 * PEGApplet.java
 *
 * Created on October 29, 2007, 10:58 AM
 */

package curro;

import scio.coordinate.R2;
import scio.coordinate.R3;
import sequor.editor.ComboBoxEditor;
import specto.euclidean2.PointSet2D;
import specto.euclidean3.PointSet3D;
import utility.SimulationFactory;

/**
 *
 * @author  ae3263
 */
public class PEGApplet extends javax.swing.JApplet {
       
    /** Initializes the applet PEGApplet */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();    
                    plot2D1.getVisometry().setBounds(new R2(-70,-70),new R2(70,70));

                    plot3D1.getVisometry().setBounds(new R2(-150,-150),new R2(150,150));
                    plot3D1.getVisometry().setTDir(new R3(-.28,.73,-.623));
                    plot3D1.getVisometry().setNDir(new R3(.9,.436,0));
                    plot3D1.getVisometry().setBDir(new R3(-.272,.561,.779));
                    plot3D1.getVisometry().setSceneSize(100);
                    plot3D1.getVisometry().setViewDist(150);

                    metricsPlot.getVisometry().setBounds(new R2(-10,-10),new R2(500,150));
                    metricsPlot.synchronizeTimerWith(plot2D1);
                    plot3D1.synchronizeTimerWith(plot2D1);

                    mainVisuals1.setSim(simulation1);
                    mainVisuals3D1.setSim(simulation1);
                    metricVisuals1.setSim(simulation1);
                    plot2D1.add(mainVisuals1);
                    plot3D1.add(mainVisuals3D1);
                    metricsPlot.add(metricVisuals1);
                    metricsPlot.add(metricVisuals1.getLegend(),5,2);
                    simulation1.run();
                    simulationComboBox.setModel(new ComboBoxEditor(simulation1.getGameTypeModel()));
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        mainVisuals1 = new analysis.MainVisuals();
        mainVisuals3D1 = new analysis.MainVisuals3D();
        metricVisuals1 = new analysis.MetricVisuals();
        simulationLog1 = new analysis.SimulationLog();
        simulation1 = new simulation.Simulation();
        jSplitPane2 = new javax.swing.JSplitPane();
        jSplitPane1 = new javax.swing.JSplitPane();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        plot2D1 = new specto.euclidean2.Plot2D();
        plot3D1 = new specto.euclidean3.Plot3D();
        metricsPlot = new specto.euclidean2.Plot2D();
        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        simulationComboBox = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        resetButton = new javax.swing.JButton();
        runButton = new javax.swing.JButton();
        simulationSettingsPanel1 = new applications.SimulationSettingsPanel(simulation1.ss);

        simulation1.setGameType(2);
        simulation1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simulation1ActionPerformed(evt);
            }
        });

        jSplitPane2.setDividerLocation(450);
        jSplitPane2.setOneTouchExpandable(true);

        jSplitPane1.setDividerLocation(400);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setOneTouchExpandable(true);

        jTabbedPane2.setPreferredSize(new java.awt.Dimension(305, 228));

        plot2D1.setAutoscrolls(true);
        plot2D1.setAxisStyle(1);
        plot2D1.setMarkerBoxVisible(false);

        javax.swing.GroupLayout plot2D1Layout = new javax.swing.GroupLayout(plot2D1);
        plot2D1.setLayout(plot2D1Layout);
        plot2D1Layout.setHorizontalGroup(
            plot2D1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 416, Short.MAX_VALUE)
        );
        plot2D1Layout.setVerticalGroup(
            plot2D1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 313, Short.MAX_VALUE)
        );

        jTabbedPane2.addTab("2D", plot2D1);

        plot3D1.setZLabel("time");
        plot3D1.setAxisStyle(3);
        plot3D1.setControlsVisible(false);
        plot3D1.setMarkerBoxVisible(false);

        javax.swing.GroupLayout plot3D1Layout = new javax.swing.GroupLayout(plot3D1);
        plot3D1.setLayout(plot3D1Layout);
        plot3D1Layout.setHorizontalGroup(
            plot3D1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 416, Short.MAX_VALUE)
        );
        plot3D1Layout.setVerticalGroup(
            plot3D1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 313, Short.MAX_VALUE)
        );

        jTabbedPane2.addTab("3D", plot3D1);

        jSplitPane1.setLeftComponent(jTabbedPane2);

        metricsPlot.setAnimatorVisible(false);
        metricsPlot.setMarkerBoxVisible(false);
        metricsPlot.setMinimumSize(new java.awt.Dimension(100, 100));

        javax.swing.GroupLayout metricsPlotLayout = new javax.swing.GroupLayout(metricsPlot);
        metricsPlot.setLayout(metricsPlotLayout);
        metricsPlotLayout.setHorizontalGroup(
            metricsPlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 421, Short.MAX_VALUE)
        );
        metricsPlotLayout.setVerticalGroup(
            metricsPlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jSplitPane1.setRightComponent(metricsPlot);

        jSplitPane2.setRightComponent(jSplitPane1);

        jToolBar1.setFloatable(false);

        jLabel1.setText("Scenario:");
        jToolBar1.add(jLabel1);

        simulationComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        simulationComboBox.setMaximumSize(new java.awt.Dimension(150, 20));
        jToolBar1.add(simulationComboBox);
        jToolBar1.add(jSeparator1);

        resetButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        resetButton.setText("RESET");
        resetButton.setToolTipText("Click here to reset the simulation and re-randomize starting positions.");
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(resetButton);

        runButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        runButton.setText("RUN!!");
        runButton.setToolTipText("Click here to run the simulation again.");
        runButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(runButton);

        simulationSettingsPanel1.setDividerLocation(100);
        simulationSettingsPanel1.setSim(simulation1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
            .addComponent(simulationSettingsPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(simulationSettingsPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE))
        );

        jSplitPane2.setLeftComponent(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 879, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void simulation1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simulation1ActionPerformed
        //System.out.println("pegplot action performed: "+evt.getActionCommand());
        if(evt.getActionCommand().equals("redraw")){
            plot2D1.repaint();
            plot3D1.repaint();
            metricsPlot.repaint();
            simulationSettingsPanel1.repaint();
        }
        // simulation has changed in some fundamental way
        else if(evt.getActionCommand().equals("reset")){
            plot2D1.rebuildOptionsMenu();
            plot3D1.rebuildOptionsMenu();
            metricsPlot.rebuildOptionsMenu();
            simulationSettingsPanel1.setTree(simulation1.ss);
            if(simulation1.getGameType()==SimulationFactory.FLOCKING) {
                mainVisuals1.setAnimationStyle(PointSet2D.ANIMATE_DOT);
                mainVisuals1.setStartVisible(false);
                mainVisuals1.setNetworkVisible(false);
                mainVisuals1.setPositionsVisible(false);
                mainVisuals3D1.setAnimationStyle(PointSet3D.ANIMATE_DOT);
                mainVisuals3D1.setPositionsVisible(false);
                mainVisuals3D1.setStartVisible(false);
            }
        }
    }//GEN-LAST:event_simulation1ActionPerformed

    private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
        simulation1.initStartingLocations();
        simulation1.run();
    }//GEN-LAST:event_resetButtonActionPerformed

    private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonActionPerformed
        simulation1.run();
    }//GEN-LAST:event_runButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JToolBar jToolBar1;
    private analysis.MainVisuals mainVisuals1;
    private analysis.MainVisuals3D mainVisuals3D1;
    private analysis.MetricVisuals metricVisuals1;
    private specto.euclidean2.Plot2D metricsPlot;
    private specto.euclidean2.Plot2D plot2D1;
    private specto.euclidean3.Plot3D plot3D1;
    private javax.swing.JButton resetButton;
    private javax.swing.JButton runButton;
    private simulation.Simulation simulation1;
    private javax.swing.JComboBox simulationComboBox;
    private analysis.SimulationLog simulationLog1;
    private applications.SimulationSettingsPanel simulationSettingsPanel1;
    // End of variables declaration//GEN-END:variables
        
}
